package com.longdaji.decoration.ui.comment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.Comment;
import com.longdaji.decoration.utils.ImageUtil;
import com.longdaji.decoration.widget.BetterRecycleView;
import com.longdaji.decoration.widget.CustomSnapHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class CriticismAdapter extends RecyclerView.Adapter<CriticismAdapter.ViewHolder> {
    private CriticismListener mListener;
    private Context mContext;
    private List<Comment> mCommentList;

    public interface CriticismListener {

        void onLikeClick(Comment comment, int position);

    }

    public CriticismAdapter(Context context, List<Comment> commentList, CriticismListener listener) {
        if (commentList == null)
            commentList = new ArrayList<>();
        this.mCommentList = commentList;
        this.mContext = context;
        this.mListener = listener;
    }

    public void setData(List<Comment> commentList) {
        if (commentList == null)
            commentList = new ArrayList<>();
        this.mCommentList = commentList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_criticism, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final Comment comment = mCommentList.get(position);
        ImageUtil.loadCircleImage(mContext, comment.getHeadImgLocation(),R.mipmap.default_head_portrait_mine, holder.head);
        // GlideUtils.loadImage(mContext, comment.getHeadImgLocation(), holder.head, null, R.drawable.no_banner, R.drawable.no_banner);
        holder.userName.setText(comment.getUserName());
        holder.goodsName.setText(comment.getGoodsName());
        holder.commentsLike.setText(comment.getCommentLike() + "");
        if (TextUtils.isEmpty(comment.getCommentContent())) {
            holder.commentsContent.setVisibility(View.GONE);
        } else {
            holder.commentsContent.setVisibility(View.VISIBLE);
            holder.commentsContent.setText(comment.getCommentContent());
        }
        Drawable drawable;
        if ("0".equals(comment.getIsLike())) {
            drawable = mContext.getResources().getDrawable(R.mipmap.before_like);
            holder.commentsLike.setTextColor(ContextCompat.getColor(mContext, R.color.important_txt_color));
        } else {
            drawable = mContext.getResources().getDrawable(R.mipmap.after_like);
            holder.commentsLike.setTextColor(ContextCompat.getColor(mContext, R.color.red));
        }

        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        holder.commentsLike.setCompoundDrawables(drawable, null, null, null);
        holder.commentsLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.onLikeClick(comment, position);
            }
        });
        if (comment.getCommentImgs() != null && comment.getCommentImgs().size() > 0) {
            holder.recycleView.setVisibility(View.VISIBLE);
            holder.recycleView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            holder.recycleView.setAdapter(new CardRvAdapter(mContext, comment.getCommentImgs()));
            CustomSnapHelper mMySnapHelper = new CustomSnapHelper();
            try {
                mMySnapHelper.attachToRecyclerView(holder.recycleView);
            } catch (Exception e) {

            }
        } else {
            holder.recycleView.setVisibility(View.GONE);
        }
    }

    public Comment getItemByPosition(int position) {
        return mCommentList.get(position);
    }

    @Override
    public int getItemCount() {
        return mCommentList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView userName;
        TextView goodsName;
        TextView commentsLike;
        TextView commentsContent;
        BetterRecycleView recycleView;
        ImageView head;


        public ViewHolder(View itemView) {
            super(itemView);
            userName = itemView.findViewById(R.id.tv_username);
            goodsName = itemView.findViewById(R.id.tv_goods_comment_name);
            commentsLike = itemView.findViewById(R.id.tv_comments_like);
            commentsContent = itemView.findViewById(R.id.tv_comment_content);
            recycleView = itemView.findViewById(R.id.better_recycle_view);
            head = itemView.findViewById(R.id.iv_head);
        }
    }

}
